#include "initFunc.h"
#include "utils.h"
#include "../_public.h"
#include "cmdParser.h"

int initThreadControl(status_st *threadControl, pthread_t *threadIds)
{
    // 初始化队列
    void *p = initQueue();
    memcpy(&threadControl->q, p, sizeof(threadControl->q));
    // 初始化互斥锁
    pthread_mutex_init(&threadControl->mutex, NULL);
    // 初始化条件变量
    pthread_cond_init(&threadControl->cond, NULL);
    // 线程池线程数
    threadControl->threadNum = THREADNUM;
    // 设置退出标记位
    threadControl->exitFlag = 0;

    // 创建线程
    for (int i = 0; i < threadControl->threadNum; i++)
    {
        pthread_create(&threadControl->thread_id[i], NULL, worker, (void *)threadControl);
    }

    return 0;
}

int initSockBindListen(int *listenFd, char *ip, int port)
{
    // printf("ip:%s\tport:%d\n",ip,port);
    *listenFd = socket(AF_INET, SOCK_STREAM, 0);
    int reuse = 1;
    setsockopt(*listenFd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
    ERROR_CHECK(*listenFd, -1, "socket");
    struct sockaddr_in addr_in4;
    addr_in4.sin_family = AF_INET;
    addr_in4.sin_addr.s_addr = inet_addr(ip);
    addr_in4.sin_port = htons(port);
    int ret = bind(*listenFd, (struct sockaddr *)&addr_in4, sizeof(addr_in4));
    ERROR_CHECK(ret, -1, "bind");
    ret = listen(*listenFd, 5);
    ERROR_CHECK(ret, -1, "listen");
    return 0;
}

int getSocktLink(int listen_fd, int *socket_fd)
{
    // 获取连接
    struct sockaddr_in addr_client; // 获取连上来的客户端ip和端口
    memset(&addr_client, 0, sizeof(addr_client));
    socklen_t addr_len = sizeof(addr_client);
    *socket_fd = accept(listen_fd, (struct sockaddr *)&addr_client, &addr_len);
}

int addEpollObj(int epollFd, int fd)
{
    struct epoll_event event;
    memset(&event, 0, sizeof(event));
    event.data.fd = fd;
    event.events = EPOLLIN;
    epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &event);
    return 0;
}

int getAbsPath(qNode_st* node,char* absPath)
{
    char path[1024] = {0};
    sprintf(absPath,"%s/%s%s",SERVER_PATH,node->username,node->curPath);
    return 1;
}

void *worker(void *args)
{
    // printf(" worker \n");
    status_st *threadControl = (status_st *)args;
    queue_st *q = &threadControl->q;
    while (1)
    {
        // 从队列中取一个对象出来
        // 加锁
        pthread_mutex_lock(&threadControl->mutex);
        printf("73-->q.size:%d\n", q->size);

        while (isEmpty(q) == true)
        {
            // 阻塞
            printf("进入阻塞...\n");
            pthread_cond_wait(&threadControl->cond, &threadControl->mutex);
            if (threadControl->exitFlag == -1)
            {
                pthread_mutex_unlock(&threadControl->mutex);
                pthread_exit(NULL);
            }
        }
        printf("解锁...\n");
        //  printf(" worker3 \n");
        // 取出元素
        qNode_st node1;
        qNode_st* node = deQueue(q,&node1);
        int socketFd = node->fd;
        printf("连上来的socketFd:%d\n", socketFd);
        printf("连上来的node:%d\n", node->fd);
        printf("node->curPath:%s\n",node->curPath);
        printf("node->username:%s\n",node->username);
        printf("拿到一个对象...\n");
        printf("91-->q.size:%d\n", q->size);
        // 唤醒生产者
        pthread_cond_signal(&threadControl->cond);
        // 解锁
        pthread_mutex_unlock(&threadControl->mutex);

        // sleep(10);
        // TODO...做事中
        char recv_buf[1024];
        char send_buf[1024];
        serv2clientMsg_t returnMsg;
        while (1)
        {
            memset(recv_buf, 0, sizeof(recv_buf));
            memset(send_buf, 0, sizeof(send_buf));
            // strcpy(send_buf,">");
            // send(socketFd,send_buf,strlen(send_buf),0);

            printf("等待接收命令..\n...\n");
            int count = recv(socketFd, recv_buf, sizeof(recv_buf), 0);
            if (count == 0)
            {
                printf("客户端关闭...\n");
                break;
            }
            printf("recv_buf:%s\n", recv_buf);
            printf("处理中...\n");

            // 拆分字符串
            int tokenSize;
            char **tokens = splitFunc(recv_buf, &tokenSize);


            // 对拆分的字符串if-else进入不同的函数
            char command[1024] = {0};
            memcpy(command, tokens[0], strlen(tokens[0]));
            printf("command:%s\n", command);

            // 得到绝对路径
            char clientabspath[1024] = { 0 };
            getAbsPath(node,clientabspath);
            printf("148:clientabspath:%s\n", clientabspath);

            serv2clientMsg_t  returnMsg;
            if (strcmp(command, "cd") == 0)
            {
                printf("处理cd命令..\n");
                // returnMsg = cdFunc(tokens,"路径",tokenSize);
            }
            else if(strcmp(command, "ls") == 0)
            {
                printf("处理ls命令..\n");
            }
            else if(strcmp(command, "puts") == 0)
            {
                printf("处理puts命令..\n");
                // returnMsg = putsFunc(tokens,"路径",tokenSize,socketFd,"文件大小");
            }
            else if(strcmp(command, "gets") == 0)
            {
                printf("处理gets命令..\n");
            }
            else if(strcmp(command, "remove") == 0)
            {
                printf("处理remove命令..\n");
            }
            else if(strcmp(command, "pwd") == 0)
            {
                printf("处理pwd命令..\n");
            }
            else if(strcmp(command, "mkdir") == 0)
            {
                printf("处理mkdir命令..\n");
                returnMsg = mkdirFunc(tokens,clientabspath,tokenSize);
            }
            else
            {
                printf("无效命令..\n");
            }
            

            // 发送结构体给客户端
            // send(socketFd, "ok", 2, 0);
            send(socketFd, &returnMsg, sizeof(returnMsg), 0);

        }
        printf("-->进程退出124\n");
        // printf("做事中...\n");
    }

    return NULL;
}

